home *** CD-ROM | disk | FTP | other *** search
-
-
-
- ffffiiiieeeellllddddssss((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) ffffiiiieeeellllddddssss((((3333))))
-
-
-
- NNNNAAAAMMMMEEEE
- fields - compile-time class fields
-
- SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
- {
- package Foo;
- use fields qw(foo bar _private);
- }
- ...
- my Foo $var = new Foo;
- $var->{foo} = 42;
-
- # This will generate a compile-time error.
- $var->{zap} = 42;
-
- {
- package Bar;
- use base 'Foo';
- use fields 'bar'; # hides Foo->{bar}
- use fields qw(baz _private); # not shared with Foo
- }
-
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- The fields pragma enables compile-time verified class
- fields. It does so by updating the %FIELDS hash in the
- calling package.
-
- If a typed lexical variable holding a reference is used to
- access a hash element and the %FIELDS hash of the given type
- exists, then the operation is turned into an array access at
- compile time. The %FIELDS hash map from hash element names
- to the array indices. If the hash element is not present in
- the %FIELDS hash, then a compile-time error is signaled.
-
- Since the %FIELDS hash is used at compile-time, it must be
- set up at compile-time too. This is made easier with the
- help of the 'fields' and the 'base' pragma modules. The
- 'base' pragma will copy fields from base classes and the
- 'fields' pragma adds new fields. Field names that start
- with an underscore character are made private to a class and
- are not visible to subclasses. Inherited fields can be
- overridden but will generate a warning if used together with
- the -w switch.
-
- The effect of all this is that you can have objects with
- named fields which are as compact and as fast arrays to
- access. This only works as long as the objects are accessed
- through properly typed variables. For untyped access to
- work you have to make sure that a reference to the proper
- %FIELDS hash is assigned to the 0'th element of the array
- object (so that the objects can be treated like an pseudo-
-
-
-
- Page 1 (printed 10/23/98)
-
-
-
-
-
-
- ffffiiiieeeellllddddssss((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) ffffiiiieeeellllddddssss((((3333))))
-
-
-
- hash). A constructor like this does the job:
-
- sub new
- {
- my $class = shift;
- no strict 'refs';
- my $self = bless [\%{"$class\::FIELDS"], $class;
- $self;
- }
-
-
- SSSSEEEEEEEE AAAALLLLSSSSOOOO
- the _b_a_s_e manpage, the section on _P_s_e_u_d_o-_h_a_s_h_e_s: _U_s_i_n_g _a_n
- _a_r_r_a_y _a_s _a _h_a_s_h in the _p_e_r_l_r_e_f manpage
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 2 (printed 10/23/98)
-
-
-
-